<?php

class Filter{
	private $tickets;
	private $getStops = array();
	private $carriers = array();
	private $lowestPrice;
	private $stops = array();
	private $maxTravelTime = array();
	private $minTravelTime = array("9999","59");
	
	public function Filter($ticketsObj){
		$this->tickets = $ticketsObj;
		$this->setValues();
	}
	
	private function setValues(){
		foreach($this->tickets as $ticket){
		
			//Carriers
			$carrier[$ticket->operatingCarrier] =  $ticket->totalPrice;
			$this->carriers +=  $carrier;
			
			//Stops
			$stops[$ticket->outboundStops] =  $ticket->totalPrice;
			$this->getStops +=  $stops;
			$pieces = explode(":" ,$ticket->journeyTime);
			

			if($pieces[0] >= $this->maxTravelTime[0]){
				if($pieces[0]  == $this->maxTravelTime[0] && $pieces[1] > $this->maxTravelTime[1]){
					$this->maxTravelTime =  array($pieces[0] ,$pieces[1] );
				}
				else{
					$this->maxTravelTime =  array($pieces[0] ,$pieces[1] );
				}
			}
			
			if($pieces[0]  <= $this->minTravelTime[0] && $pieces[1]  < $this->minTravelTime[1]){
				$this->minTravelTime =  array($pieces[0] ,$pieces[1] );
			}
		}
	}
	
	public function setTickets($tickets){
		$this->tickets = $tickets;
	}
	
	private function getTickets(){
			return $this->tickets;
	}
	
	public function getMaxTime(){
		return  $this->maxTravelTime;
	}
	
	public function getMinTime(){
		return  $this->minTravelTime;
	}
	
	
		
	public function travelTime($begintijd,$eindtijd){
		$tickets = $this->getTickets();
		 foreach($tickets as $ticket){
			$ticketBeginTijd = $ticket->journeyTime;
			$time = explode(":",$ticketBeginTijd);
			$time= $time[0] . $time[1];
		
			if(($begintijd < $time) && ($eindtijd > $time)){
				$ticket->filter['travelTime'] = true;
			}else{
				$ticket->filter['travelTime'] = false;
			} 
		} 
		return $tickets;
	}
	
	
	public function betweenDepartureTime($beginTijd, $eindTijd){
		$tickets = $this->getTickets();
	
		foreach($tickets as $ticket){
			$ticketBeginTijd = $ticket->flights_outbound[0]['fromDateTime']->format("hi");
			if(($beginTijd < $ticketBeginTijd) && ($eindTijd > $ticketBeginTijd)){
				$ticket->filter['timeArrival'] = true;
			}else{
				$ticket->filter['timeArrival'] = false;
			} 
		}
		return $tickets;
	}
	
	public function betweenReturnTime($beginTijd, $eindTijd){
		$tickets = $this->getTickets();
		
		foreach($tickets as $ticket){

			$ticketBeginTijd = $ticket->flights_inbound[0]['fromDateTime']->format("hi");

			if(($beginTijd < $ticketBeginTijd) && ($eindTijd > $ticketBeginTijd)){
				$ticket->filter['timeReturn'] = true;
			}else{
				$ticket->filter['timeReturn'] = false;
			} 
			
		}
		return $tickets;
	}
	
	public function getLowestPrice(){

		$this->lowStopPrice = array("url" => $this->tickets[0]->url,
														   "price" => $this->tickets[0]->totalPrice);
										 
		return $this->lowStopPrice;
	}
	
	public function disableStops($stops){
		$tickets = $this->getTickets();
	
		foreach($tickets as $ticket){
			foreach($stops as $stop => $value){
				if($value == 0){
					//disable
					if($ticket->outboundStops == $stop){
						$ticket->filter['stop'] = false;
					}
				}
				if($value == 1){
					//disable
					if($ticket->outboundStops == $stop){
						$ticket->filter['stop'] = true;
					}
				}
			} 
		}
		return $tickets;
	}
	
	public function getStops(){
		return $this->getStops;
	}
	
	public function getCarriers(){
		return $this->carriers;
	}
	
	public function disableCarrier($carriers){
		$tickets = $this->getTickets();
		foreach($tickets as $ticket){
			foreach($carriers as $carrier => $value){
				if($value == 0){
					if($ticket->operatingCarrier == $carrier){
							$ticket->filter['carrier'] = false;
					}
				}
				if($value == 1){
					//disable
					if($ticket->operatingCarrier == $carrier){
						$ticket->filter['carrier'] = true;
					}
				}
			}
		}
		return $tickets;
	}

}


?>